//
//  ZJKRetweetStatusView.swift
//  MicroBlog
//
//  Created by VAD on 16/4/20.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit
import SnapKit

class ZJKRetweetStatusView: UIView {
    
    // 当前控件的底部约束：需要根据是否有配图去动态更新
    var bottomCons: Constraint?


    var statusViewModel: ZJKStatusViewModel?{
        didSet{
            
            //设置转发微博内容
            contentLabel.text = statusViewModel?.retweetText
            
            bottomCons?.uninstall()
            
            //设置配图数据
            //如果有配图
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
                //给配图设置数据
                pictureView.dataInfo = (pic_urls,statusViewModel!.retweetPictureViewSize)
                
                //把配图显示出来
                pictureView.hidden = false
                
                //转发微博视图的底部等于配图视图的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomCons = make.bottom.equalTo(pictureView).offset(ZJKStatusCellMargin).constraint
                })
            }else{//如果没有配图
                
                //把配图隐藏
                pictureView.hidden = true
                
                //转发微博视图的底部等于文字label的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentLabel).offset(ZJKStatusCellMargin).constraint
                })
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(ZJKStatusCellMargin)
        }
        
        //添加配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(ZJKStatusCellMargin)
            
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(ZJKStatusCellMargin).constraint
        }
        
    }
    
    // MARK: - 懒加载控件
    
    // 转发微博显示内容的lable
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZJKStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * ZJKStatusCellMargin)
    
    /// 配图视图
    private lazy var pictureView: ZJKStatusPictureView = ZJKStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    

}
